package com.jd.seg;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;

public class DictUtil {
	
	private static Set<String> dicSet = new HashSet<String>();
	
	private static Set<String> unitsDicSet = new HashSet<String>();
	
	private static Map<String,int[]>  wordsLen= new HashMap<String, int[]>();
	
	static{
		loadDicSet("dict/dict");
		loadUnitsDic("dict/units");
		loadWordsLen("dictionary");
		System.out.println(wordsLen.size());
	}
	
	public static void loadDicSet(String dicPath){
		
		BufferedReader br = null;
		try{
			br = new BufferedReader(new InputStreamReader(new FileInputStream(DictUtil.class
						.getClassLoader()
						.getResource(dicPath)
						.getFile()),"utf-8"));
			String line;
			while((line=br.readLine())!=null){
				dicSet.add(line);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			try{
				br.close();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
		System.out.println("dic load success,have been load "+dicSet.size()+ " words!");
		
	}
	
	public static void loadUnitsDic(String dicPath){
		
		BufferedReader br = null;
		try{
			br = new BufferedReader(new InputStreamReader(new FileInputStream(DictUtil.class
						.getClassLoader()
						.getResource(dicPath)
						.getFile()),"utf-8"));
			String line;
			while((line=br.readLine())!=null ){
				if(!line.startsWith("#"))
//				System.out.println(line);
					unitsDicSet.add(line);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			try{
				br.close();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
		System.out.println("unitsDicSet load success,have been load "+unitsDicSet.size()+ " words!");
		
	}
	
	
	public static void loadWordsLen(String path){

			for(String dic:dicSet){
				char[] chars = dic.toCharArray();
				int dicLen = chars.length;
				String startWord = new String(ArrayUtils.subarray(chars, 0, 1));
				String endWord = new String(ArrayUtils.subarray(chars, dicLen-1, dicLen));
				
				if(wordsLen.containsKey(startWord)){
					if(wordsLen.get(startWord)[0] < dicLen){
						wordsLen.put(startWord, new int[]{dicLen,wordsLen.get(startWord)[1]});
					}
					
				}else{
					wordsLen.put(startWord, new int[]{dicLen,0});
				}
				
				if(wordsLen.containsKey(endWord)){
					if(wordsLen.get(endWord)[1]<dicLen){
						wordsLen.put(endWord, new int[]{wordsLen.get(endWord)[0],dicLen});
					}
				}else{
					wordsLen.put(endWord, new int[]{0,dicLen});
				}
			}
			System.out.println("load success.");
			
			

	}
	
	/**
	 * 获取词典中，以word开头的词的最大长度。需要事先统计好，在初始化的时候加载
	 * @param word
	 * @return
	 */
	public static int getMaxLenStartBy(String word){
		if(wordsLen.containsKey(word)){
			return wordsLen.get(word)[0];
		}
		return 0;
	}
	
	/**
	 * 获取词典中,以word结尾的词的最大长度，需要事先统计好，在初始化的时候加载。
	 * @param word
	 * @return
	 */
	public static int getMaxLenEndBy(String word){
		if(wordsLen.containsKey(word)){
			return wordsLen.get(word)[1];
		}
		return 0;
	}
	
	/**
	 * 词是否在分词词典中
	 * @param word
	 * @return
	 */
	public static boolean match(String word){
		if(dicSet.contains(word)){
			return true;
		}
		return false;
	}
	
	/**
	 * 词是否是单位词
	 * @param word
	 * @return
	 */
	public static boolean inUnitsDic(String word){
		if(unitsDicSet.contains(word)){
			return true;
		}
		return false;
	}

}
